from DataPrepare.dailyFactorsWithInfluxdb.factorBase import factorBase
import cx_Oracle as oracle
import pandas as pd
from Config.myConfig import *
from Config.myConstant import *
from DataAccess.TradedayDataProcess import TradedayDataProcess
########################################################################
class marketValue(factorBase):
    """记录日线的行情数据等"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='dailyMarketValue'
        pass
    #----------------------------------------------------------------------
    def getDataFromOracleByCode(self,code,startDate,endDate):
        data=self.__getDailyDerivativeDataByDateFromOracleServer(code,startDate,endDate)
        return data
        pass
    #----------------------------------------------------------------------
    #输入code=600000.SH，startdate=yyyyMMdd，endDate=yyyyMMdd
    def __getDailyDerivativeDataByDateFromOracleServer(self,code,startDate=EMPTY_STRING,endDate=EMPTY_STRING):
        #获取衍生数据
        #1表示涨停；0表示非涨停或跌停；-1表示跌停。
        database='wind_filesync.AShareEODDerivativeIndicator'
        connection = oracle.connect(self.oracleConnectStr)
        cursor = connection.cursor()
        oracleStr="select  S_INFO_WINDCODE as code,TRADE_DT as \"date\", S_VAL_MV as totalMarketValue,S_DQ_MV as freeMarketValue,S_VAL_PE_TTM as PE,S_VAL_PS_TTM as PS,S_DQ_FREETURNOVER as turnover,TOT_SHR_TODAY as totalShares,FREE_SHARES_TODAY as freeShares,UP_DOWN_LIMIT_STATUS as limitStatus from wind_filesync.AShareEODDerivativeIndicator "
        if startDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' order by TRADE_DT".format(code)
        elif endDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} order by TRADE_DT".format(code,startDate)
        else:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} and TRADE_DT<={2} order by TRADE_DT".format(code,startDate,endDate)
        cursor.execute(oracleStr)
        myderivativedata = cursor.fetchall()
        myderivativedata = pd.DataFrame(myderivativedata,columns=['code','date','totalMarketValue','freeMarketValue','PE','PS','turnover','totalShares','freeShares','limitStatus'])
        if (myderivativedata.shape[0]==0):
            return myderivativedata
        mytradedays=TradedayDataProcess.getAllTradedays()
        myderivativedata=myderivativedata[myderivativedata['date'].isin(mytradedays)]
        myderivativedata[['totalMarketValue','freeMarketValue','PE','PS','turnover','totalShares','freeShares','limitStatus']] = myderivativedata[['totalMarketValue','freeMarketValue','PE','PS','turnover','totalShares','freeShares','limitStatus']].astype('float')
        myderivativedata['mytime']=pd.to_datetime(myderivativedata['date'],format='%Y%m%d')
        myderivativedata.set_index('mytime',inplace=True,drop=True)
        return myderivativedata
########################################################################
